# -*- coding: UTF-8 -*-
import scrapy
import messAround.util.help as util


# 猫眼电影热门话题
# https://maoyan.com/news
# scrapy crawl maoyan_topic
class MaoyanTopSpider(scrapy.Spider):
    name = 'maoyan_topic'

    allowed_domains = ['maoyan.com']

    start_urls = ['https://maoyan.com/news']

    custom_settings = {
        'ITEM_PIPELINES': {
            'messAround.pipeline.maoyan.MaoyanTopicPipeline': 300
        }
    }

    def start_requests(self):
        url = 'https://maoyan.com/news'
        yield scrapy.Request(url=url, callback=self.parse, headers=util.default_headers)

    def parse(self, response):
        for index in range(1, 11):
            if index == 1:
                item_xpath = f'/html/body/div[4]/div/div[1]/div[1]/ul/li[{index}]/div/p/a'
                title = response.xpath(f'{item_xpath}/@title').get()
            else:
                item_xpath = f'/html/body/div[4]/div/div[1]/div[1]/ul/li[{index}]/div/div/p/a'
                title = response.xpath(f'{item_xpath}/text()').get()

            link = response.xpath(f'{item_xpath}/@href').get()

            yield {
                'no': index,
                'title': title,
                'link': f'https://maoyan.com{link}',
            }
        pass
